package com.hainatong.shiphelper.module.warehouse.fragment;

import java.util.ArrayList;
import java.util.List;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentChukuTaskBinding;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.module.warehouse.entity.ChukuGoodsParam;
import com.hainatong.shiphelper.module.warehouse.entity.ImpaBean;
import com.hainatong.shiphelper.module.warehouse.entity.WareHouseWuzhiBean;
import com.hainatong.shiphelper.module.warehouse.viewmodel.RuKuViewModel;
import com.hainatong.shiphelper.utils.TimeUtil;
import com.hainatong.shiphelper.widget.LoadingDialog;
import com.hainatong.shiphelper.widget.PickerDialog;
import com.hainatong.shiphelper.widget.datapicker.CustomDatePicker;
import com.jeremyliao.liveeventbus.LiveEventBus;

import android.os.Bundle;
import android.view.View;


public class ChukuGoodsFragment extends MvvmLazyFragment<FragmentChukuTaskBinding, RuKuViewModel>
{
    private static String RUKU_TASK_TAG = "RukuTaskFragment";
    public static String FINISH_ADD_GOODS = "finishAddGoods";
    //ImpaShelfCodeBean
    private String impaCode;
    private ImpaBean theImpa;
    private List<String> piciList;

    private List<String> wareNameList;

    private List<WareHouseWuzhiBean> wareHouseList;

    private LoadingDialog loading;

    private PickerDialog pickerDialog;

    private CustomDatePicker datePicker;

    private ChukuGoodsParam chukuGoods;

    private String selectPiCiStr = "";
    private int selectTag = -1;

    public static ChukuGoodsFragment newInstance(String scanCode)
    {
        Bundle bundle = new Bundle();
        bundle.putString( RUKU_TASK_TAG , scanCode );
        ChukuGoodsFragment taskFragment = new ChukuGoodsFragment();
        taskFragment.setArguments( bundle );
        return taskFragment;
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_chuku_task;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected void initParameters()
    {
        Bundle args = getArguments();
        if(  args != null )
        {
            impaCode = args.getString(RUKU_TASK_TAG , "");
        }
    }

    @Override
    protected void onRetryBtnClick()
    {
    }

    @Override
    protected void initView(View view)
    {
        loading = new LoadingDialog( getActivity(), getString( R.string.loading ) );
        chukuGoods = new ChukuGoodsParam();
        wareNameList = new ArrayList<String>();
        // 选择 批次号
        pickerDialog = new PickerDialog(getActivity(), (selectStr, position) -> {
            if( selectTag == 1 )
            {
                chukuGoods.setWarehouse_id( wareHouseList.get(position).getUuid() );
                LogUtils.e("出库商品仓库ID = " + wareHouseList.get(position).getUuid()  );
                viewDataBinding.tvChukuWarehouse.setText( selectStr );
            }
            else if ( selectTag == 2 )
            {
                //批次号
                selectPiCiStr = selectStr;

                chukuGoods.setBatchNo( selectStr );

                viewDataBinding.tvPiciTime.setText( selectStr );
            }
            ToastUtils.showShort( selectStr );
        });
        initDataPicker();
        //批次
        viewDataBinding.btnPiciSelect.setOnClickListener( v -> {
            selectTag = 2;
            if(  piciList != null && !piciList.isEmpty()  )
            {
                //已经 获取过 批次了
                pickerDialog.showPickContent(piciList);
            }
            else
            {
                if( !StringUtils.isEmpty( impaCode ) )
                {
                    loading.show();
                    viewModel.queryImpaPici( impaCode);
                }
            }
        } );
        // 选择 仓库
        viewDataBinding.btnChukuWarehouseSelect.setOnClickListener( v -> {
            selectTag = 1;
            if( wareNameList != null && !wareNameList.isEmpty() )
            {
                //已经 获取过 仓库了
                pickerDialog.showPickContent(wareNameList);
            }
            else
            {
                /*
                if(  StringUtils.isEmpty( selectPiCiStr )  )
                {
                    ToastUtils.showShort( "先选择批次" );
                    return;
                }
                */
                if( !StringUtils.isEmpty( impaCode ) )
                {
                    loading.show();
                    viewModel.queryWareHouseByImpaCode( impaCode);
                }
            }
        } );

        //添加信息 到列表
        viewDataBinding.btnAddStr.setOnClickListener( v -> {
            //
            String countStr = viewDataBinding.etCountSum.getText().toString();
            if( StringUtils.isEmpty( countStr ) )
            {
                ToastUtils.showShort( "输入数量" );
                return;
            }
            int count = Integer.parseInt( countStr );

            chukuGoods.setCount( count );

            //添加 仓库 批次信息

            LiveEventBus.get(FINISH_ADD_GOODS, ChukuGoodsParam.class)
                        .post( chukuGoods );
            //关闭页面
            getActivity().finish();
        } );
        // 跳转 破损提交
        viewDataBinding.btnBroken.setOnClickListener( v -> {

            // 跳转 破损提交   物资ID
            Bundle bundle = new Bundle();
            bundle.putString( Constants.FRAGMNET_TYPE, Constants.VIEW_TYPE_BROKEN_SUB);
            bundle.putString( Constants.SCAN_CONTENT_CODE, impaCode);
            ComFragmentActivity.start( getActivity(),bundle );

        } );
    }

    @Override
    protected void lazyLoad()
    {
        if(!StringUtils.isEmpty( impaCode ))
        {
            //查询 物资
            viewModel.queryImpaByCode( impaCode );
        }
        //拿到 物资信息 后
        viewModel.getImpaResult().observe(this , result -> {
            if( result.isSuccess() )
            {
                theImpa = result.getData();
                // 列表里的 入库信息
                // 设置 物资名称等
                chukuGoods.setImpaBean(theImpa);

                viewDataBinding.setImpabean(result.getData());
                viewDataBinding.executePendingBindings();
            }
            else
            {
                ToastUtils.showShort(result.getMsg());
            }
        });

        // 批次信息
        viewModel.getImpaBatchResult().observe(this,result -> {
            loading.hide();
            if( result.isSuccess() )
            {
                piciList = result.getData();
                pickerDialog.showPickContent(piciList);
            }
            else
            {
                ToastUtils.showShort( result.getMsg()  );
            }
        });
        // 仓库信息
        viewModel.getWareResult().observe(this,result -> {
            loading.hide();
            if( result.isSuccess() )
            {
                wareNameList.clear();
                wareHouseList = result.getData();
                for(int p = 0;p < wareHouseList.size() ;p++ )
                {
                  wareNameList.add( wareHouseList.get(p).getShelfName() );
                }
                pickerDialog.showPickContent(wareNameList);
            }
            else
            {
                ToastUtils.showShort( result.getMsg() );
            }
        });

    }
    // 没有用到
    private void initDataPicker()
    {
        datePicker = new CustomDatePicker(getActivity(), new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time)
            {

                //.setText( TimeUtil.getTimeYMDHS2YMD( time ) );
                //rukuParam.setCreateDate( TimeUtil.getTimeYMDHS2YMD( time )  );
                // 获取时间 一个long值
                //long resultTime = TimeUtil.parseDate(time).getTime();
                LogUtils.e("CustomDatePicker", time + "  时间= " + TimeUtil.getTimeYMDHS2YMD( time ));
            }
        } , "2000-01-01 00:00" ,  TimeUtil.getTimeStandard((System.currentTimeMillis() )));
        // + 1000*60*60*24*365*5
        // 初始化日期格式请用：yyyy-MM-dd HH:mm
        // HH:mm，否则不能正常运行 //false 不显示时和分
        datePicker.showSpecificTime(false);
        datePicker.setIsLoop(true); // false 不允许循环滚动
        /*
         * //currentTime.setText(now); datePicker = new CustomDatePicker(this, new
         * CustomDatePicker.ResultHandler() {
         * @Override public void handle(String time) { // 回调接口，获得选中的时间 //currentTime.setText(time);
         * } }, "2010-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
         * datePicker.showSpecificTime(true); // 显示时和分 datePicker.setIsLoop(true); // 允许循环滚动
         */
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        loading.dismiss();
    }
}
